package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ProductDetailConventer;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author: DYQ
 * @time: Created on 2022/6/19 16:00
 */

@Service
public class IproductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ProductDetailConventer productDetailConventer;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();

        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productDetailConventer.item2ProductDetailDto(item);
            String[] images = item.getImages();
            productDetailDto.setProductImageSmall(Arrays.asList(images));

            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDetailDto(productDetailDto);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public PageResponse getAllProduct(AllProductRequest request) {

        PageResponse response = new PageResponse();

        request.requestCheck();

        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();

        if (request.getPriceGt() != null) {
            criteria.andGreaterThan("price", request.getPriceGt());
        }
        if (request.getPriceLte() != null) {
            criteria.andLessThan("price", request.getPriceLte());
        }
        if (request.getSort() == null) {
            String orderByClause = "created desc";
            example.setOrderByClause(orderByClause);
        } else if (request.getSort().equals("-1")) {
            String orderByClause = "price desc";
            example.setOrderByClause(orderByClause);
        } else if (request.getSort().equals("1")) {
            String orderByClause = "price asc";
            example.setOrderByClause(orderByClause);
        }

        PageHelper.startPage(request.getPage(), request.getSize());

        List<Item> items = itemMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(items);

        List<ProductDto> productDtos = productDetailConventer.items2ProductDtos(items);

        response.setData(productDtos);
        response.setTotal(pageInfo.getTotal());

        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        RecommendResponse response = new RecommendResponse();

        Panel panel = panelMapper.getRecommendGoods();
        List<PanelContentItem> panelContentItems = panel.getPanelContentItems();
        List<PanelContentItemDto> panelContentItemDtos = productDetailConventer.panelContentItems2PanelContentItemDto(panelContentItems);

        PanelDto panelDto = productDetailConventer.panel2PanleDto(panel);
        panelDto.setPanelContentItems(panelContentItemDtos);
        Set<PanelDto> panelDtos = new HashSet<>();
        panelDtos.add(panelDto);
        response.setPanelContentItemDtos(panelDtos);

        return response;
    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}
